using KinonekoSoftware.CodariaDB.Languages;

namespace KinonekoSoftware.CodariaDB.Documents.Timelines
{
    partial class TimelineSystem
    {
        //-------------------------------------------------------------
        //
        //         Readonly  Fields
        //
        //-------------------------------------------------------------


        //-------------------------------------------------------------
        //
        //          Fields
        //
        //-------------------------------------------------------------


        //-------------------------------------------------------------
        //
        //          Private Methods
        //
        //-------------------------------------------------------------
        
        //-------------------------------------------------------------
        //
        //          Public Methods
        //
        //-------------------------------------------------------------

        public void SavePlotChanges()
        {
            _system.Update(Plot);
        }

        public void Add(Plot plot)
        {
            if (plot is null)
            {
                return;
            }

            //
            //
            PlotCollection.Add(plot);
            _system.Add(plot);
        }

        public void Update(Plot plot) => _system.Update(plot);

        public void Remove(Plot plot)
        {
            
            if (plot is null)
            {
                return;
            }

            //
            // 在剧情碎片集合中删除
            PlotCollection.Remove(plot);
            
            //
            // 保存到数据库
            _system.Remove(plot);
        }
        
        
        //-------------------------------------------------------------
        //
        //          Rebuild
        //
        //-------------------------------------------------------------
        public void RebuildPlots()
        {
            var plots = _system.GetPlots();
            
            //
            //
            PlotCollection.AddMany(plots, true);

            //
            //
            var plot = PlotCollection.FirstOrDefault();

            if (plot is null)
            {
                plot = new Plot
                {
                    Id     = ID.GetShortID(),
                    Name   = "未命名",
                    Intro  = "未命名",
                    Points = new List<string>(8),
                };
                
                //
                //
                Add(plot);
                OnPlotChanged(plot);
                RebuildStoryPoint(plot);
            }
            else
            {
                RebuildStoryPoint(plot);
                OnPlotChanged?.Invoke(plot);
            }
        }
        
        //-------------------------------------------------------------
        //
        //          Events
        //
        //-------------------------------------------------------------
        public Action<Plot> OnPlotChanged { get; set; }
        
        //-------------------------------------------------------------
        //
        //          Properties
        //
        //-------------------------------------------------------------

        //-------------------------------------------------------------
        //
        //          UI
        //
        //-------------------------------------------------------------
        public Plot Plot { get; set; }

        public ObservableCollection<Plot> PlotCollection { get; }


    }
}